<?php

/**
 * The Authentication logic
 *
 * @author andrea
 */
class Storefront_Service_Authentication 
{
    /**
     *
     * @var Zend_Auth_Adapter_DbTable
     */
    protected $_authAdapter;
    
    /**
     *
     * @var Storefront_Model_User
     */
    protected $_userModel;
    
    /**
     *
     * @var Zend_Auth
     */
    protected $_auth;
    
    /**
     * Creates or sets the User MOdel instance
     * 
     * @param Storefront_Model_User $userModel 
     */
    public function __construct(Storefront_Model_User $userModel = NULL) 
    {
        $this->_userModel = NULL === $userModel ?
                new Storefront_Model_User() : $userModel;
    }
    
    /**
     * Process the authentication request.
     *
     * @param array $credentials
     * @return boolean 
     */
    public function authenticate($credentials)
    {
        $adapter = $this->getAuthAdapter($credentials);
        $auth = $this->getAuth();
        $result = $auth->authenticate($adapter);
        
        if (!$result->isValid()) {
            return FALSE;
        }
        
        $user = $this->_userModel->getUserByEmail($credentials['email']);
        
        $auth->getStorage()->write($user);
        
        return TRUE;
    }
    
    /**
     * Return the Zend_Auth instance.
     *
     * @return Zend_Auth
     */
    public function getAuth()
    {
        if (NULL === $this->_auth) {
            $this->_auth = Zend_Auth::getInstance(); //Zend_Auth is a singleton class
        }
        
        return $this->_auth;
    }
    
    /**
     * Returns the stored identity
     *
     * @return mixed|null
     */
    public function getIdentity()
    {
        $auth = $this->getAuth();
        if ($auth->hasIdentity()) {
            return $auth->getIdentity();
        }
        
        return FALSE;
    }
    
    /**
     * Clear the identity (log out)
     */
    public function clear()
    {
        $this->getAuth()->clearIdentity();
    }
    
    /**
     * Set the authentication adapter to use
     *
     * @param Zend_Auth_Adapter_Interface $adapter 
     */
    public function setAuthAdapter(Zend_Auth_Adapter_Interface $adapter)
    {
        $this->_authAdapter = $adapter;
    }
    
    /**
     * Configures the authentication adapter
     *
     * @param array $values
     * @return Zend_Auth_Adapter_DbTable 
     */
    public function getAuthAdapter($values)
    {
        if (NULL === $this->_authAdapter) {
            $authAdapter = new Zend_Auth_Adapter_DbTable(
                    Zend_Db_Table_Abstract::getDefaultAdapter(),
                    'user',
                    'email',
                    'passwd'
            );
            $this->setAuthAdapter($authAdapter);
            $this->_authAdapter->setIdentity($values['email']);
            $this->_authAdapter->setCredential($values['passwd']);
            $this->_authAdapter->setCredentialTreatment(
                    'SHA1 (CONCAT(?,salt))'
            );
        }
        
        return $this->_authAdapter;
    }
}
